using System;
using System.Collections.Generic;
using FPLibrary;
using FrameOperateSync;
using Pvp.Ecs;
using Pvp.Stage;
using UnityEngine;
using UnityEngine.SceneManagement;
using Behaviour = FrameOperateSync.Behaviour;

namespace Pvp
{
    public class EcsStartup : MonoBehaviour,IFrameLoger,IFrameParser
    {
        [SerializeField]
        private NetworkClient network;
        private void Start()
        {
            Behaviour.BindUnityBehaviour<EcsBehaviour>();
            
#if UNITY_EDITOR
            startUpSync = true;
#endif
        }

        [SerializeField]
        private bool startUpSync = false;

        [SerializeField]
        private bool singleMode = true;
        
        private void Update()
        {
            if (startUpSync)
            {
                startUpSync = false;
                StarUpSync(network, this, this, singleMode);
            }
        }

        private void FixedUpdate()
        {
            RenderUtility.GameFixedUpdaterRender();
        }
        
        private void OnDestroy()
        {
            RenderUtility.DisposeGame();
        }
        
        #region IFrameLog & IFrameParser
        public void LogInfo(string msg)
        {
            Debug.Log(msg);
            //GameFramework.GameFrameworkLog.Info(msg);
        }

        public void LogWarning(string msg)
        {
            Debug.LogWarning(msg);
            //GameFramework.GameFrameworkLog.Warning(msg);
        }

        public void LogError(string msg)
        {
             Debug.LogError(msg);
            //GameFramework.GameFrameworkLog.Error(msg);
        }

        public object FromJson(string json, Type type)
        {
            return JsonUtility.FromJson(json, type);
        }

        public string ToJson(object data)
        {
            return JsonUtility.ToJson(data);
        }

        static void StarUpSync(NetworkClient network, IFrameLoger logger, IFrameParser parser,bool singleFlag)
        {
            HelperUtility.SetLogger(logger);
            HelperUtility.SetFrameParser(parser);
            HelperUtility.SetSetting("siglePlayerMode", singleFlag);
            if (!HelperUtility.SinglePlayerMode)
            {
                network.StartNetwork();
                ClientUtility.StartSync(network, null, a => { a?.Invoke(); });
            }
            else
            {
                Debug.Log("单机模式启动");
                ClientUtility.StartSync(null, null, a => { a?.Invoke(); });
                GameObject.Destroy(network);
            }
        }
        #endregion

        private class EcsBehaviour : IBehaviour
        {
            private GameStage _GameStage;

            protected override void Awake()
            {
                _GameStage = new GameStage(null);
            }

            protected override void Start()
            {
                CreateStageSingleComponent();
                
                // Ecs.Entity.CreateEntity((int)EntityType.gear,(int)GearType.门机关1); 
                // Ecs.Entity.CreateEntity((int)EntityType.gear, (int)GearType.地刺机关1);
                // Ecs.Entity.CreateEntity((int)EntityType.gear, (int)GearType.地刺机关2);
                // Ecs.Entity.CreateEntity((int)EntityType.gear, (int)GearType.地刺机关3);
                // Ecs.Entity.CreateEntity((int)EntityType.monster,(int)MonsterType.怪物1);
                // Ecs.Entity.CreateEntity((int)EntityType.hero,(int)HeroType.男,101);
                // Ecs.Entity.CreateEntity((int)EntityType.hero,(int)HeroType.男,102);
                // Ecs.Entity.CreateEntity((int)EntityType.hero,(int)HeroType.男,103);
                
                _GameStage.CreateStageEntity();

                Systemes.Start();
            }

            protected override void FrameUpdate(int renderFrameIndex)
            {
                Systemes.Update(renderFrameIndex);
            }

            private void CreateStageSingleComponent()
            {
                Ecs.Entities.AddSingleComponent<IStage>();
                Ecs.Entities.AddSingleComponent<IPlayerInput>();
            }
        }
    }
}
